package zcj.stu.service;

import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import zcj.stu.Entity.Goods;
import zcj.stu.Entity.Order;
import zcj.stu.dao.OrderDao;
import zcj.stu.utils.Result;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;

@Service
public class OrderService {
    @Resource
    private MongoTemplate mongoTemplate;
    @Resource
    private OrderDao orderDao;

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");

    public Result add(Order order) {
        //验库存
        List<Order.Goods> selectedCart = order.getSelectedCart();
        if (selectedCart == null) return Result.error("商品不能为空");
        for (Order.Goods goods : selectedCart) {
            if (!mongoTemplate.exists(Query.query(Criteria.where("_id").is(goods.get_id()).and("num").gte(goods.getNum())), Goods.class)) {
                return Result.error("订单商品库存不足");
            }
        }
        //减库存
        for (Order.Goods goods : selectedCart) {
            Query query = Query.query(Criteria.where("_id").is(goods.get_id()));
            Update update = new Update();
            update.inc("num", -goods.getNum());
            mongoTemplate.updateFirst(query, update, Goods.class);
        }
        //插入订单
        order.setOrder_status("unpay");
        String addTime = simpleDateFormat.format(new Date());
        order.setOrder_add_time(addTime);
        orderDao.add(order);
        return Result.ok();
    }

    public List<Order> getOrderByStatus(String openid, String status) {
        Criteria criteria = Criteria.where("userInfo.openid").is(openid);
        if (!"all".equals(status)) {
            criteria.andOperator(Criteria.where("order_status").is(status));
        }
        Sort.Order order =new Sort.Order(Sort.Direction.DESC, "order_add_time");
        Query query = Query.query(criteria).with(Sort.by(order));
        return orderDao.getOrderByStatus(query);
    }

    public boolean updateStatus(Map<String, String> map) {
        if (map == null) return false;
        Query query = Query.query(Criteria.where("_id").is(map.get("_id")));
        Update update = new Update();
        update.set("order_status", map.get("order_status"));
        return orderDao.updateStatus(query,update).getModifiedCount()>0;
    }

    public Result saleStatistics() {
        List<Order> orders=new ArrayList<>();
        Criteria criteria0 = Criteria.where("_id").exists(true).and("order_status").is("payOk");
        List<Order> orders0 = orderDao.getOrderByStatus(Query.query(criteria0));
        if(orders0!=null)
            orders.addAll(orders0);
        Criteria criteria1 = Criteria.where("_id").exists(true).and("order_status").is("confirm");
        List<Order> orders1 = orderDao.getOrderByStatus(Query.query(criteria1));
        if(orders1!=null)
            orders.addAll(orders1);
        Criteria criteria2 = Criteria.where("_id").exists(true).and("order_status").is("finished");
        List<Order> orders2 = orderDao.getOrderByStatus(Query.query(criteria2));
        if(orders2!=null)
            orders.addAll(orders2);
        HashMap<String, Integer> map = new HashMap<>();
        List<String>x=null;
        List<Integer>y=null;
        if(orders!=null&&orders.size()>0){
            x=new ArrayList<>();
            y=new ArrayList<>();
            for (Order order : orders) {
                List<Order.Goods> selectedCart = order.getSelectedCart();
                for (Order.Goods goods : selectedCart) {
                    int pre=0;
                    Integer integer = map.get(goods.getGoods_name());
                    if(integer!=null)pre+=integer;
                    map.put(goods.getGoods_name(),pre+(int)goods.getNum());
                }
            }
            for (String key : map.keySet()) {
                Integer num = map.get(key);
                x.add(key);
                y.add(num);
            }
        }
        HashMap<String, List> result = new HashMap<>();
        result.put("x",x);
        result.put("y",y);
        return Result.ok(result);
    }
}
